package com.tinckay.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.api.R;
import com.tinckay.common.annotation.Log;
import com.tinckay.common.constant.ProjectConstant;
import com.tinckay.entity.po.Role;
import com.tinckay.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.extension.api.ApiController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jean
 * @since 2018-10-30
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController extends ApiController {

    @Autowired
    private RoleService roleService;

    @Log("删除角色")
    @DeleteMapping
    @ApiOperation("删除角色")
    //@RequiresPermissions("role:delete")
    public R delete(@ApiParam("角色id数组") @RequestParam Long[] ids) {
        if (ArrayUtil.isEmpty(ids)) {
            return failed("参数id不能为空.");
        }
        roleService.deleteRoles(CollectionUtil.toList(ids));
        return success(null);
    }

    @Log("新增角色")
    @PostMapping
    @ApiOperation("新增角色")
    //@RequiresPermissions("role:add")
    public R add(@RequestBody @Validated(ProjectConstant.Save.class) Role role,
                 @RequestParam(required = false) Long[] menuIds) {
        roleService.addRole(role, menuIds);
        return success(role);
    }

    @Log("修改角色")
    @PutMapping
    @ApiOperation("修改角色")
    //@RequiresPermissions("role:update")
    public R update(@RequestBody @Validated(ProjectConstant.Update.class) Role role,
                    @RequestParam(required = false) Long[] menuIds) {
        roleService.updateRole(role, menuIds);
        return success(role);
    }

    @GetMapping("/list")
    @ApiOperation("查询角色列表")
    //@RequiresPermissions("role:list")
    public R list(@RequestParam(required = false) String roleName) {
        List<Role> result = roleService.findAllRole(roleName);
        return success(result);
    }

    @GetMapping("/info")
    @ApiOperation("查询角色权限信息")
    //@RequiresPermissions("role:info")
    public R info(@RequestParam Long roleId) {
        Role role = this.roleService.findRoleWithMenus(roleId);
        return success(role);
    }

}

